import { createRouter, createWebHistory } from 'vue-router';

const routes =[
    {
        name:'home',
        path:'/',
        component:()=>import("@/views/components/index.vue"),
        children:[
            {
                path:'/',
                name:'index',
                component:()=>import("@/views/home/Index.vue")
            },
            {
                name: 'anime',
                path: '/GV:id',
                component:() =>import("@/views/anime/Index.vue"),
                props: true
            },
            {
                name: 'animePlay',
                path: '/playGV:id-1-:playid',
                component:() =>import("@/views/videoPlay/Index.vue"),
                props: true
            },
            {
                path:'/animeFilter',
                name:'animeFilter',
                component:()=>import("@/views/anime/animeFilter.vue")
            },
            {
                name: 'MovieFilter',
                path:'/MovieFilter',
                component:() =>import("@/views/Movie/MovieFilter.vue")
            },
            {
                name: 'animeHistory',
                path:'/animeHistory',
                component:() =>import("@/views/animeHistory/Index.vue")
            },
            {
                name: 'userCenter',
                path: '/user',
                component: () => import("@/views/userCenter/iudex.vue"),
                meta: { requiresAuth: true },
                redirect: '/user/account',
                children: [
                    {
                        path: 'account',
                        component: () => import("@/views/userCenter/components/AccountInfo.vue"),
                        name: 'AccountInfo'
                    },
                    {
                        path: 'history',
                        component: () => import("@/views/userCenter/components/WatchHistory.vue"),
                        name: 'WatchHistory'
                    },
                    {
                        path: 'login-history',
                        component: () => import("@/views/UserCenter/components/LoginHistory.vue"),
                        name: 'LoginHistory'
                    }
                ]
            }
        ]
    },
    {
       path: '/admin',
       name: 'admin',
       component: () => import('@/views/admin/home/index.vue'),
       redirect: '/admin/home',
       children:[
           {
               path: '/admin/home',
               name: 'anime_home',
               component: () => import('@/views/admin/Dashboard/index.vue'),
           },
           {
               path: '/admin/anime/List',
               name: 'admin_animeList',
               component: () => import('@/views/admin/anime/animeList.vue'),
           },
           {
               path: '/admin/anime/files',
               name: 'admin_user_files',
               component: () => import('@/views/admin/anime/files.vue'),
           },
           {
               path: '/admin/error',
               name: 'error',
               component: () => import('@/views/error/error.vue'),
           },
           {
               path: '/admin/adminUser/userInfo',
               name: 'admin_user_info',
               component: () => import('@/views/admin/adminUser/userInfo.vue'),
           },
           {
               path: '/admin/user/list',
               name: 'admin_user_list',
               component: () => import('@/views/admin/user/userList.vue'),
           },
           {
               path: '/admin/user/vip',
               name: 'admin_user_vip',
               component: () => import('@/views/admin/user/userVipList.vue'),
           },
           {
               path: '/admin/user/adminuser',
               name: 'admin_user_adminuser',
               component: () => import('@/views/admin/user/adminuser.vue'),
           },
           {
               path: '/admin/user/operation',
               name: 'admin_user_operation',
               component: () => import('@/views/admin/user/operation.vue'),
           },
           {
               path: '/admin/user/superadmin',
               name: 'admin_user_superadmin',
               component: () => import('@/views/admin/user/superadmin.vue'),
           },
           {
               path: '/admin/:pathMatch(.*)*',
               name: 'admin_404',
               component: () => import('@/views/error/error.vue'),
               props: (route) => ({
                   errorType: '404',
                   errorCode: '404'
               })
           }
       ]
    },
    {
        path: '/admin/login',
        name: 'admin_login',
        component: () => import('@/views/admin/account/login.vue'),
    },
    {
        path: '/error',
        name: 'error',
        component: () => import('@/views/error/error.vue'),
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/error',
        props: (route) => ({
            errorType: '404',
            errorCode: '404'
        })
    }
]

// 创建router实例
const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition // 恢复历史滚动位置‌
        } else {
            return { top: 0 } // 新导航跳转顶部‌
        }
    }
});

// 路由守卫
router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('token')
    if (to.path.startsWith('/admin/home') && !isAuthenticated) {
        next('/admin/login')
    }
    if(to.path.startsWith('/user') && !isAuthenticated) {
        next("/")
    }
    next()
})

export default router;